package com.note.app.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.note.app.dto.TagDTO;
import com.note.app.service.TagService;
import com.note.common.context.UserContext;
import com.note.common.vo.ResponseVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import com.note.app.annotation.LogRecord;

import java.util.List;

/**
 * 标签控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/tags")
@RequiredArgsConstructor
@Tag(name = "标签管理", description = "标签相关接口")
public class TagController {

    private final TagService tagService;

    @PostMapping
    @Operation(summary = "创建标签", description = "创建一个新的标签")
    @LogRecord(description = "创建标签", module = "标签管理", operationType = LogRecord.OperationType.CREATE)
    public ResponseVo<Long> createTag(
            @Parameter(description = "标签名称") @RequestParam String name) {
        Long userId = UserContext.getUserId();
        return ResponseVo.success(tagService.createTag(userId, name));
    }

    @PutMapping("/{tagId}")
    @Operation(summary = "更新标签", description = "更新标签名称")
    @LogRecord(description = "更新标签", module = "标签管理", operationType = LogRecord.OperationType.UPDATE)
    public ResponseVo<Boolean> updateTag(
            @Parameter(description = "标签ID") @PathVariable Long tagId,
            @Parameter(description = "新标签名称") @RequestParam String name) {
        Long userId = UserContext.getUserId();
        return ResponseVo.success(tagService.updateTag(userId, tagId, name));
    }

    @DeleteMapping("/{tagId}")
    @Operation(summary = "删除标签", description = "根据标签ID删除标签")
    @LogRecord(description = "删除标签", module = "标签管理", operationType = LogRecord.OperationType.DELETE)
    public ResponseVo<Boolean> deleteTag(
            @Parameter(description = "标签ID") @PathVariable Long tagId) {
        Long userId = UserContext.getUserId();
        return ResponseVo.success(tagService.deleteTag(userId, tagId));
    }

    @GetMapping
    @Operation(summary = "获取用户标签列表", description = "分页获取当前用户的标签列表")
    public ResponseVo<Page<TagDTO>> getUserTags(
            @Parameter(description = "当前页码") @RequestParam(defaultValue = "1") long current,
            @Parameter(description = "每页条数") @RequestParam(defaultValue = "20") long size) {
        Long userId = UserContext.getUserId();
        return ResponseVo.success(tagService.getUserTags(userId, current, size));
    }

    @GetMapping("/all")
    @Operation(summary = "获取所有用户标签", description = "获取当前用户的所有标签")
    public ResponseVo<List<TagDTO>> getAllUserTags() {
        Long userId = UserContext.getUserId();
        return ResponseVo.success(tagService.getAllUserTags(userId));
    }

    @GetMapping("/note/{noteId}")
    @Operation(summary = "获取笔记标签", description = "获取指定笔记的所有标签")
    public ResponseVo<List<TagDTO>> getTagsByNoteId(
            @Parameter(description = "笔记ID") @PathVariable Long noteId) {
        return ResponseVo.success(tagService.getTagsByNoteId(noteId));
    }
} 